package org.gangcai.mac.shop.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.api.AppAPI;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.UserDataBean;
import org.gangcai.mac.shop.bean.UserInfoBean;
import org.gangcai.mac.shop.common.BaseActivity;
import org.gangcai.mac.shop.common.BaseApplication;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.manager.RetrofitManageres;
import org.gangcai.mac.shop.manager.RetrofitManagers;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.utils.PromptDialogUtils;
import org.gangcai.mac.shop.utils.UserInfoUtils;

import java.util.function.Consumer;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.leefeng.promptlibrary.PromptDialog;

import static org.gangcai.mac.shop.constants.Constant.CODE_SUCC;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.forgetPassword)
    TextView forgetPassword;
    @BindView(R.id.login)
    Button openAlive;
    @BindView(R.id.activity_login)
    View activityLogin;
    @BindView(R.id.iphone)
    EditText iphone;
    @BindView(R.id.password)
    EditText password;
    private PromptDialog prompDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        prompDialog = PromptDialogUtils.getPrompDialog(this);

    }


    private static final String TAG = "LoginActivity";

    @OnClick({R.id.forgetPassword, R.id.login, R.id.registTv})
    public void onViewClicked(View view) {


        switch (view.getId()) {
            case R.id.forgetPassword:
                ActivityUtils.startActivity(this, FindPasswordActivity.class);
                break;
            case R.id.registTv:
                ActivityUtils.startActivity(this, RegisterActivity.class);
                break;
            case R.id.login:
                prompDialog.showLoading("请等待");
                RetrofitManageres.getInstance()
                        .create(AppAPI.class)
                        .LoginData(iphone.getText().toString(), password.getText().toString())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new CommonObserver<UserDataBean>(prompDialog) {
                            @Override
                            public void onSuccess(UserDataBean value) {
                                if (value.getCode() == CODE_SUCC) {
                                    BaseApplication.getInstance().setLoginUser(value.getReferer());
                                    SPUtils.getInstance().put("uid", value.getReferer().getId());
                                    SPUtils.getInstance().put("phoneNumber", iphone.getText().toString());
                                    UserInfoUtils.setAppUserId( value.getReferer().getId(),LoginActivity.this );
                                    UserInfoUtils.setNikeName( value.getReferer().getUser_nicename() ,LoginActivity.this);
                                    UserInfoUtils.setPhone( iphone.getText().toString(),LoginActivity.this );
                                    UserInfoUtils.setAppUserUrl( value.getReferer().getAvatar() ,LoginActivity.this);
                                    JPushInterface.setAlias(LoginActivity.this, 1, value.getUid());
//                                    ActivityUtils.startActivity(LoginActivity.this, MainActivity.class);
                                    finish();
                                }
                                Log.e(TAG, "onSuccess: ");
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                            }
                        });


                break;
                default:
                    break;
        }
    }

}
